//
//  SecButtonCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/25.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "SecButtonCon.h"




@interface SecButtonCon ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *listCollectionview;
@property(nonatomic,strong)NSArray *imageArray;
@end

@implementation SecButtonCon

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self showDetailImageDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:self.urlStr]];
    [self creatCollectionViewMethod];
}

-(void)creatCollectionViewMethod{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.listCollectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    self.listCollectionview.backgroundColor = [UIColor whiteColor];
    self.listCollectionview.dataSource = self;
    self.listCollectionview.delegate = self;
    [self.listCollectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.listCollectionview];
    
    
    
}


-(void)showDetailImageDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.imageArray = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listCollectionview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}


#pragma mark -- collertionView.dataSource
//返回item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
    
    
}
//返回cell 对于每一item返回一个cell对象（重用机制）
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSDictionary *subdic = self.imageArray[indexPath.row];
    NSString *urlstr = subdic[@"GOODS_PIC"];
    UIImageView *imagevi = [UIImageView new];
    NSURL *uRL = [NSURL URLWithString:[MethodClass returnUrlSetStrWSDLURL:urlstr]];
    [imagevi sd_setImageWithURL:uRL];
    [cell.contentView addSubview:imagevi];
    imagevi.sd_layout
    .topSpaceToView(cell.contentView, 15*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 25*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 25*kJLXWidthScale)
    .heightIs(cell.contentView.frame.size.height - 90*kJLXWidthScale);
    
    UILabel *titlab = [UILabel new];
    titlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    titlab.textAlignment = NSTextAlignmentCenter;
    titlab.textColor = [UIColor blackColor];
    titlab.numberOfLines = 3;
    titlab.text = [NSString stringWithFormat:@"%@",subdic[@"GOODS_NAME"]];
    [cell.contentView addSubview:titlab];
    titlab.sd_layout
    .topSpaceToView(imagevi, 0)
    .rightSpaceToView(cell.contentView, 10*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 10*kJLXWidthScale)
    .heightIs(60*kJLXWidthScale);
    
    UILabel *pricelab = [UILabel new];
    pricelab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    pricelab.textAlignment = NSTextAlignmentLeft;
    pricelab.textColor = [UIColor orangeColor];
    pricelab.text = subdic[@"GOODS_MINPRICE"];
    [cell.contentView addSubview:pricelab];
    pricelab.sd_layout
    .topSpaceToView(titlab, 0)
    .rightSpaceToView(cell.contentView, 10*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 10*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale);
    return cell;
}
//返回分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

#pragma mark -- UICollectionViewDelegate
//点击item时
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
//动态设置每个item的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth/2 - 2, (kScreenWidth/2 - 2) + 70*kJLXWidthScale);
    
    
    
}
//最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
//最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}



////动态返回每个分区页脚的尺寸
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
//    return section == 0 ? CGSizeMake(100, 100) :CGSizeMake(200, 200);
//}
//动态设置item的缩进量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 1, 0, 1);
    
}


@end
